<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\index\controller;
use mip\ApiBase;
use mip\AuthApiBase;
use mip\Init;
use think\Controller;
use think\Request;
use think\Response;
use think\Session;
use mip\Mip;
use com\Network;
use app\common\controller\Common;
use WeMini;
use WeChat\Script;


class ApiAnonyWechat extends Init
{
    public function getMipToken(){

        if(!isset( $this->wxcfg)){
            $msg =  '由于未知原因，未能获得wxJsSign';
        }

        $callback = input('callback');

        if(isset($msg)){
            $arrRes = ['errno'=>1241,'errmsg'=>$msg];
            $jsonRes = json_encode($arrRes);
            $res = $callback .'('.$jsonRes.')';
            return $res;
        }

        $url = $_SERVER['HTTP_REFERER'];

        // 初始化wxscript
        $wxscript=new Script($this->wxcfg);
        $jssign = $wxscript->getJsSign( $url );

        $wx_appid = $this->mipInfo['wx_appid'];
        $nonceStr = $jssign['nonceStr'];
        $timestamp = $jssign['timestamp'];
        $signature = $jssign['signature'];

        $arrRes = ['errno'=>0,'errmsg'=>'SUCCESS','time'=>$timestamp, 'hasFlush' => true,'format'=>'jsonp',
            'data'=>[ 'appId'=>$wx_appid, 'nonceStr'=> $nonceStr, 'timestamp'=> $timestamp,'url'=>$url,'signature' => $signature ]];
        $jsonRes = json_encode($arrRes);

        $res = $callback.'('.$jsonRes.')';
        return $res;

    }


}
